<template>
  <DataTable searchPlaceholder="请输入账号或姓名查询" :columns="columns" dialogTitle="用户信息表" :formFields="formFields"
    :tableData="tableData" apiUrl="admin" :putUser="putUser" :addUser="addUser" :delUser="delUser" />
</template>

<script setup>
import DataTable from '../../components/DataTable.vue';
import { ref, onMounted } from "vue";
import axios from "axios";

// 定义表格列配置
const columns = ref([
  { prop: "userName", label: "账号", width: "180" },
  { prop: "nickName", label: "姓名", width: "180" },
  { prop: "jurisdiction", label: "权限", width: "180" },
  { prop: "doctorDescribe", label: "简介" }
]);

// 定义表单字段配置
const formFields = ref([
  { model: "userName", label: "账号", placeholder: "请输入账号" },
  { model: "userPasswd", label: "密码", placeholder: "请输入密码" },
  { model: "nickName", label: "姓名", placeholder: "请输入姓名" },
  { model: "jurisdiction", label: "权限", placeholder: "管理员" },
  { model: "doctorDescribe", label: "简介", placeholder: "请输入简介" }
]);

const tableData = ref([]); // 表格数据

// 获取所有管理员用户
async function getAllUser() {
  try {
    const res = await axios.get("api/hospital/getAllUser");
    tableData.value = res.data.data.filter(user => user.jurisdiction === 2);
  } catch (error) {
    console.error("获取用户数据时出错:", error);
  }
}

// 新增数据
async function addUser(formValue) {
  console.log("new")
  console.log(formValue)

  try {
    await axios.post("api/hospital/addUser", formValue);
    console.log("admin", formValue)
    location.reload()
  } catch (error) {
    console.error("新增用户时出错:", error);
  }
}

// 编辑数据
async function putUser(formValue) {
  try {
    await axios.put(`api/hospital/putUser/${formValue.id}`, formValue);
    console.log("admin编辑", formValue)
  } catch (error) {
    console.error("编辑用户时出错:", error);
  }
}

// 删除数据确
async function delUser(index, row) {
  try {
    await axios.delete(`api/hospital/delUser/${row.id}`);
    console.log("管理员信息删除：", row.id)
  } catch (error) {
    console.error("删除用户时出错:", error);
  }
}

onMounted(() => {
  getAllUser();
});
</script>
